package com.liveneo.stoat.common.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringUtilTool implements ApplicationContextAware,DisposableBean{
	private static Logger logger = LoggerFactory
			.getLogger(SpringUtilTool.class);
	private static ApplicationContext applicationContext;
	
	private static SpringUtilTool springUtilTool = null;

	public synchronized static SpringUtilTool init() {
		if (springUtilTool == null) {
			springUtilTool = new SpringUtilTool();
		}
		return springUtilTool;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	@Override
	public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
		logger.debug("注入ApplicationContext到SpringContextHolder:{}",
				applicationContext);
		if (SpringUtilTool.applicationContext != null) {
			logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
					+ SpringUtilTool.applicationContext);
		}
		SpringUtilTool.applicationContext = applicationContext;
	}
	
	/**
	 * @param name
	 *            getBean的id
	 * @return T
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(final String name) {
		return (T) applicationContext.getBean(name);
	}

	/**
	 * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 *
	 * @param requiredType
	 * @return
	 */
	public static <T> T getBean(final Class<T> requiredType) {
		T t = null;
		try {
			final String name = requiredType.getSimpleName();
			t = getBean(getStr(name));
		} catch (final Exception e) {
			t = applicationContext.getBean(requiredType);
		}
		return t;
	}

	private static String getStr(final String str) {
		final char begin = str.charAt(0);
		return String.valueOf(begin) + str.substring(1);
	}
	
	@Override
	public void destroy() throws Exception {
		clear();
	}
	
	/**
	 * 清除SpringUtilTool中的ApplicationContext为Null.
	 */
	public static void clear() {
		logger.debug("clear SpringUtilTool's  ApplicationContext:"
				+ applicationContext);
		applicationContext = null;
	}

}
